﻿using System.Collections.Generic;
using System.Linq;
using Battle.Net.Model;

namespace Battle.Net.Managers
{
    public class AuctionManager
    {
         internal AuctionManager()
        {

        }

        internal BattleNetConnection Connection { get; set; }

        public RealmAuctions GetLatest(string realm)
        {
            var auctionFile = Connection.Get<RealmAuctions>(WebRequests.AuctionFile, realm);
            if (auctionFile != null && auctionFile.Files.Count > 0)
            {

                var file = auctionFile.Files.OrderByDescending(f => f.LastModified).FirstOrDefault();
                if (file != null && file.Url != null)
                {
                    var auction = Connection.Get<RealmAuctions>(WebRequests.Auctions,file.Url);
                    auction.Files = new List<AuctionFile>();
                    auction.Files.Add(file);
                    return auction;
                }
            }

            return null;
        }
    }
}
